package com.promfy.timetable.db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.promfy.timetable.domain.Reminders;
import com.promfy.timetable.domain.Reminders;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class ReminderDBService {
	private static final String TAG = "mt_dbsv";

	private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	private ReminderDBOpenHelper dbHelper;
	private SQLiteDatabase db;
	private ContentResolver resolver;
	public ReminderDBService(Context context) {
		dbHelper = new ReminderDBOpenHelper(context);
	}

	/**
	 * get all syslog
	 */
	public List<Reminders> getList()  throws Exception{
		db = dbHelper.getWritableDatabase();
		List<Reminders> remindersList = new ArrayList<Reminders>();
		String sql = "SELECT  _id, time, content,status FROM reminders ORDER BY _id DESC";
		Cursor cursor = null;
		try {
			cursor = db.rawQuery(sql, null);
			while (cursor.moveToNext()) {
			
				Reminders reminders = new Reminders(
						cursor.getInt(cursor.getColumnIndex("_id")),
						Long.parseLong(cursor.getString(cursor.getColumnIndex("time"))), 
						cursor.getString(cursor.getColumnIndex("content")), 
						cursor.getInt(cursor.getColumnIndex("status")));
				remindersList.add(reminders);
			}
		}catch (Exception e) {
			e.printStackTrace();
		} 
		finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return remindersList; 
	}
	
	/**
	 * insert data to db
	 * @param task
	 * @throws Exception
	 */
	public void insert(Reminders reminders) throws Exception{
		db = dbHelper.getWritableDatabase();
		try {
			ContentValues cv = new ContentValues();
			cv.put("time", reminders.getTime()+"");
			cv.put("content", reminders.getContent());
			cv.put("status", reminders.getStatus());

			db.insert("reminders", null, cv);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	/**
	 * insert data to db
	 * @param task
	 * @throws Exception
	 */
	public void update(Reminders reminders) throws Exception{
		db = dbHelper.getWritableDatabase();
		try {
			ContentValues cv = new ContentValues();
			cv.put("time", reminders.getTime()+"");
			cv.put("content", reminders.getContent());
			cv.put("status", reminders.getStatus());

			db.update("reminders", cv, "_id = ?", new String[]{reminders.getId()+""});
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static String getCurrDate() {
		Calendar c = Calendar.getInstance();
		Date date = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
		String dStr = sdf.format(date);
		return dStr;
	}
	public static String getCurrDate(long time) {
		
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
		String dStr = sdf.format(new Date(time));
		return dStr;
	}
	
	public static String getComparedFormateDateString(long time) {
		String DATE_FORMAT_PATTERN1 = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN1);
		String dStr = sdf.format(new Date(time));
		return dStr;
	}
}
